<template>
  <el-card>
    <el-form :inline="true" :model="form">
      <el-form-item>
        <el-input v-model="form.real_name" :placeholder="$t('客户名称')" clearable />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.account_type"
          :placeholder="$t('客户类型')"
          clearable
          class="w-36"
        >
          <el-option
            v-for="item in accountType"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.mer_review_status"
          :placeholder="$t('商户审核状态')"
          clearable
        >
          <el-option
            v-for="item in merType"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.sys_review_status"
          :placeholder="$t('平台审核状态')"
          clearable
        >
          <el-option
            v-for="item in sysType"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :icon="Search" @click="onSubmit">{{$t('查询')}}</el-button>
        <el-button type="primary" :icon="Plus" @click="handleClickCustom"
        v-btn="['all', 'web_customer_add']">{{$t('新增客户')}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column :label="$t('客户名称')" align="center">
        <template #default="{ row }">
          {{ row.account_type === 2 ? row.real_name : row.company_name }}
        </template>
      </el-table-column>
      <el-table-column prop="account_type_name" :label="$t('客户类型')" align="center">
      </el-table-column>
      <el-table-column
        prop="create_time"
        :label="$t('创建时间')"
        align="center"
        :formatter="formatterTime"
      />
      <el-table-column prop="sys_review_status_name" :label="$t('平台审核状态')" align="center"></el-table-column>
      <el-table-column prop="mer_review_status_name" :label="$t('商户审核状态')" align="center"></el-table-column>
      <el-table-column :label="$t('银行卡')" align="center" width="180">
        <template #default="scope">
          <el-button
            round :icon="Postcard" size="small"
            v-btn="['all', 'web_customer_manage_cards']"
            @click="handleGoBankCard(scope.row)"
            v-if="scope.row.sys_review_status === 1"
            ><span style="color:#c45656;">{{ $t('银行卡管理')}}</span></el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="220">
        <template #default="scope">
          <el-button round :icon="MoreFilled" size="small"
          v-btn="['all', 'web_customer_view']"
            @click="handleDetail(scope.row)"
            v-if="scope.row.mer_review_status === 1 || scope.row.mer_review_status === 3">
           {{ $t('查看')}}
          </el-button>
          <el-button
          type="warning" round :icon="Edit" size="small"
          @click="handleEditClick(scope.row)" v-else
          v-btn="['all', 'web_customer_edit']">
            {{ $t('编辑')}}
          </el-button>
          <el-button
            type="danger" round :icon="Delete" size="small"
            v-btn="['all', 'web_customer_delete']"
            @click="handleOperate(scope.$index, scope.row, 'delete')"
            >{{ $t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-5 flex justify-center">
      <el-pagination background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageable.total"
        :default-page-size="pageable.pageSize"
        :default-current-page="pageable.pageNum"
        :page-sizes="[10, 25, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </el-card>
</template>
<script setup>
import { useRouter } from "vue-router";
import { ref, reactive } from "vue";
import { Search, MoreFilled, Plus, Delete, Edit, Postcard } from '@element-plus/icons-vue'
import { kycListApi, kycDeleteApi } from "@/api/account/index";
import { ElMessageBox, ElMessage } from "element-plus";
import { formatterTime } from "@/utils/utils";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const router = useRouter();
const loading = ref(false);
const form = reactive({
  real_name: "",
  account_type: "",
  mer_review_status: "",
  sys_review_status: "",
});

const accountType = ref([]);
const merType = ref([]);
const sysType = ref([]);
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
//新增客户
const handleClickCustom = () => {
  router.replace("/account/customer/new");
};
//银行卡管理
const handleGoBankCard = (row) => {
  // console.log(row);
  router.replace({
    path: "/account/customer/bankCard",
    query: {
      id: row.id,
//      account_type: row.account_type,
//      name:
//        row.account_type === 2
//          ? row.real_name
//          : row.company_name
//          ? row.company_name
//          : row.real_name,
    },
  });
  // router.replace(`/account/management/bankCard/${row.id}`);
};
// 表格数据
const tableData = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    real_name: form.real_name,
    account_type: form.account_type,
    mer_review_status: form.mer_review_status,
    sys_review_status: form.sys_review_status,
  };
};
const onSubmit = () => {
  getTbaleData();
};

const handleOperate = async (index, row, type) => {
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm(t("您是否确认删除?"), t("温馨提示"), {
      type: "warning",
    }).then(async () => {
      const res = await kycDeleteApi({ id: row.id });

      ElMessage({
        message: "ok",
        type: "success",
      });
      getTbaleData();
    });
  }
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    // 获取数据
    const res = await kycListApi(param());
    // console.log(res);
    loading.value = false;
    tableData.value = res.list;
    pageable.total = res.page.total_count;
    accountType.value = res.extra.filter_account_type.map((item) => {
      return { value: item.value, label: item.name };
    });
    merType.value = res.extra.filter_mer_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
    sysType.value = res.extra.filter_sys_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
  } catch (error) {
    loading.value = false;
  }
};
// getTbaleData();
Promise.all([getTbaleData()]);
// 查看
const handleDetail = ({ ...row }) => {
  router.replace({
    path: "/account/customer/details",
    query: { kycId: row.id },
  });
};
// 编辑
const handleEditClick = ({ ...row }) => {
  router.push({
    path: "/account/customer/update",
    query: {
      kycId: row.id,
      opt_type: 2,
    },
  });
};
</script>
